package club.tclyq.good_time.upload.service;

import club.tclyq.good_time.common.config.bean.ViewConfigBean;
import club.tclyq.good_time.common.utils.base64util.ImgToBase64;
import club.tclyq.good_time.upload.bean.Photo;
import club.tclyq.good_time.upload.repository.PhotoRepository;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.awt.desktop.SystemSleepEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;

/**
 * @author tanchong
 * Create Date: 2019/2/21
 */
@Service
public class UploadImgService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadImgService.class);
    private final String PHOTO_COLLECTION = "photo";
    private MongoCollection<Document> photoCollection;
    private PhotoRepository photoRepository;
    private MongoCollection<Document> photoV2Collection;
    private MongoOperations mongoOperations;
    String filePath;


    @Autowired
    public UploadImgService(MongoOperations mongoOperations,
                            PhotoRepository photoRepository,
                            ViewConfigBean viewConfigBean) {
        this.mongoOperations = mongoOperations;
        this.photoCollection = mongoOperations.getCollection(PHOTO_COLLECTION);
        this.photoRepository = photoRepository;
        filePath = viewConfigBean.getFilePath();
    }

    public void uploadImg(MultipartFile file,String title,String imgType,String description) throws NoSuchFieldException, IOException {
        if(file == null || file.isEmpty()){
            throw new NoSuchFieldException("文件识别失败");
        }
        if(imgType.equals("")){
            imgType = "default";
        }
        var base64Img = ImgToBase64.getImageStr(file);

        Document document = new Document();
        document.append("title",title)
                .append("type",imgType)
                .append("photo_path",base64Img)
                .append("description",description)
                .append("timestamp",LocalDateTime.now());
        photoCollection.insertOne(document);

    }

    public void uploadImgv2(MultipartFile file,
                            String title,
                            String imgType,
                            String description,
                            HttpServletRequest request) throws NoSuchFieldException, IOException {

        if(file == null || file.isEmpty()){
            throw new NoSuchFieldException("文件识别失败");
        }
        String filename = file.getOriginalFilename();
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName
        File newFile = new File(filePath + filename);
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
        }
        file.transferTo(newFile);
        LOGGER.info("上传成功后的文件路径未：" + filePath + filename);
    }
}
